<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>修改密码功能测试</h1>
        <form id="changePasswordForm">
            <div class="form-group">
                <label for="currentPassword">当前密码:</label>
                <input type="password" id="currentPassword" required>
            </div>
            <div class="form-group">
                <label for="newPassword">新密码:</label>
                <input type="password" id="newPassword" required>
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认新密码:</label>
                <input type="password" id="confirmPassword" required>
            </div>
            <button type="submit">修改密码</button>
        </form>
        <div id="result" class="result"></div>
    </div>

    <script>
        // 获取表单元素
        const form = document.getElementById('changePasswordForm');
        const resultDiv = document.getElementById('result');
        
        // 显示结果消息
        function showMessage(message, isSuccess) {
            resultDiv.textContent = message;
            resultDiv.className = `result ${isSuccess ? 'success' : 'error'}`;
            resultDiv.style.display = 'block';
            
            // 3秒后隐藏消息
            setTimeout(() => {
                resultDiv.style.display = 'none';
            }, 3000);
        }
        
        // 获取JWT令牌（需要用户先登录）
        function getToken() {
            // 在实际应用中，这应该从localStorage或cookie中获取
            // 这里我们使用一个示例令牌
            return localStorage.getItem('token') || '';
        }
        
        // 表单提交处理
        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            // 获取表单数据
            const currentPassword = document.getElementById('currentPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            
            // 简单验证
            if (newPassword !== confirmPassword) {
                showMessage('新密码和确认密码不匹配', false);
                return;
            }
            
            if (newPassword.length < 8) {
                showMessage('新密码长度至少为8位', false);
                return;
            }
            
            if (!/\d/.test(newPassword)) {
                showMessage('新密码必须包含至少一个数字', false);
                return;
            }
            
            try {
                // 发送修改密码请求
                const response = await fetch('/api/users/change-password', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${getToken()}`
                    },
                    body: JSON.stringify({
                        currentPassword,
                        newPassword
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    showMessage('密码修改成功!', true);
                    // 清空表单
                    form.reset();
                } else {
                    showMessage(`密码修改失败: ${data.message}`, false);
                }
            } catch (error) {
                showMessage(`请求失败: ${error.message}`, false);
            }
        });
    </script>
</body>
</html>